package com.cx.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
@Slf4j
public class GlobalErrorInfoHandler {

    //捕获什么类型的异常  也可以在 拦截器 ExceptionHandler 中处理
    @ExceptionHandler(value = {Exception.class,Throwable.class})
    @ResponseBody
    public String errorHandlerOverJson(HttpServletRequest request , HttpServletResponse res ,
                                         Exception ex, Throwable thr){
        if (ex!=null){
            log.error(ex.getMessage(),ex);
        }
        if (ex==null&&thr!=null){
            log.error(thr.getMessage(),thr);
        }

        return ex.toString();
    }



    class JsonFomat {
        private String key;
        private String value;

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}
